package barinadroid.game;

import org.andengine.entity.primitive.Rectangle;
import org.andengine.extension.physics.box2d.PhysicsConnector;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.util.color.Color;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;

public class Bullet extends GameEntity
{
	private GameEntity owner;
	private float bulletSpeed;
	private Vector2 velocity;
	private PhysicsConnector connector;

	public Bullet()
	{
		super();
		this.sprite = new Rectangle(0, 0, 50, 15, GameActivity.getSharedInstance().getVertexBufferObjectManager());
		this.sprite.setColor(Color.YELLOW);
		this.sprite.setRotationCenter(sprite.getWidth() * .5f, sprite.getHeight() * .5f);
		attachChild(sprite);
		this.velocity = new Vector2();
		this.bulletSpeed = 5f;
		setBody(EntityIdentity.BULLET);
		updateVelocity();
	}

	// @Override
	// protected void setBody(FixtureDef def)
	// {
	// PhysicsWorld physicsWorld =
	// GameScene.getSharedInstance().getPhysicsWorld();
	// if(connector != null)
	// physicsWorld.unregisterPhysicsConnector(connector);
	// this.body = PhysicsFactory.createBoxBody(physicsWorld, sprite,
	// BodyType.KinematicBody, def);
	// this.body.setBullet(true);
	// this.body.setUserData(this);
	// physicsWorld.registerPhysicsConnector(connector = new
	// PhysicsConnector(this.sprite, this.body, true, true));
	// }
	@Override
	void setBody(EntityIdentity identity)
	{
		PhysicsWorld physicsWorld = GameScene.getSharedInstance().getPhysicsWorld();
		if(connector != null)
			physicsWorld.unregisterPhysicsConnector(connector);
		//this.body = PhysicsFactory.createBoxBody(physicsWorld, sprite, BodyType.DynamicBody, identity.getFixtureDef());
		
		
		float a = sprite.getWidthScaled() * 0.5f / 32.0f;
		float b = sprite.getHeightScaled() * 0.5f / 32.0f;
		Vector2[] v =
		{new Vector2(-a, -b), new Vector2(a, -b), new Vector2(a, b), new Vector2(-a, b)};
		this.body = PhysicsFactory.createPolygonBody(physicsWorld, sprite, v, BodyType.DynamicBody, identity.getFixtureDef());
		
		
		
		
		this.body.setUserData(this);
		this.sprite.setAlpha(1);
		setEntityIdentity(identity);
		physicsWorld.registerPhysicsConnector(connector = new PhysicsConnector(this.sprite, this.body, true, true));
	}

	public Bullet ShotBy(GameEntity owner)
	{
		if(owner instanceof Ship)
			setBody(EntityIdentity.BULLET);
		else
			setBody(EntityIdentity.ENEMY_BULLET);
		setPosition(this.owner = owner);
		setAngle(owner.getAngle());
		updateVelocity(owner.getBody().getLinearVelocity());
		getBody().setAngularVelocity(owner.getBody().getAngularVelocity());
		return this;
	}

	public GameEntity getOwner()
	{
		return this.owner;
	}

	@Override
	public void setRotation(float rotation)
	{
		setAngle(rotation);
	}

	private void updateVelocity()
	{
		updateVelocity(new Vector2(0, 0));
	}

	private void updateVelocity(Vector2 baseVelocity)
	{
		float rotation = getAngle();
		this.velocity.x = baseVelocity.x + (float)(Math.cos(rotation) * bulletSpeed);
		this.velocity.y = baseVelocity.y + (float)(Math.sin(rotation) * bulletSpeed);
		getBody().setLinearVelocity(velocity);
	}
}